package com.wuwei.aliocr.listener;

import com.wuwei.aliocr.domain.Subject;
import com.wuwei.aliocr.mapper.SubjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class DemoDAO {

    @Autowired
    private SubjectMapper subjectMapper;

    public void save(List<Subject> list) {
        // 如果是mybatis,尽量别直接调用多次insert,自己写一个mapper里面新增一个方法batchInsert,所有数据一次性插入
        System.out.println("插入数据中");
        ArrayList<Subject> subjects = new ArrayList<>();
        list.forEach(a->{
            String name = a.getName();
            if (name != null) {
                String model = name.substring(0,2);
                a.setModel(model);
                subjects.add(a);
            }
        });
        subjects.forEach(a->subjectMapper.insert(a));
    }
}
